package cn.tedu.tmall.admin.mall.dao.persist.repository.impl;

import cn.tedu.tmall.admin.mall.dao.persist.mapper.CategoryMapper;
import cn.tedu.tmall.admin.mall.dao.persist.repository.ICategoryCacheRepository;
import cn.tedu.tmall.admin.mall.dao.persist.repository.ICategoryRepository;
import cn.tedu.tmall.admin.mall.pojo.po.CategoryPO;
import cn.tedu.tmall.admin.mall.pojo.vo.CategoryTreeVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Repository
public class CategoryCacheRepositoryImpl implements ICategoryCacheRepository {

    //现在我们用的是 redisTemplate 生菜
    //如果将来我们换一种客户端或者连接池 比如 jedis redission
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<CategoryTreeVO> listAllCategory() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object o = valueOperations.get(MALL_CATEGORY_CATCH_KEY);
        return (List<CategoryTreeVO>)o;
    }

    @Override
    public void saveCategory(List<CategoryTreeVO> categoryTreeVOS) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(MALL_CATEGORY_CATCH_KEY,categoryTreeVOS);
    }

    @Override
    public void saveCategory(CategoryPO categoryPO) {
        saveCategoryPO(Arrays.asList(categoryPO));
    }

    @Override
    public void saveCategoryByParentId(Long parentId, CategoryPO categoryPO) {
        saveCategoryPO(parentId,Arrays.asList(categoryPO));
    }

    @Override
    public void saveCategoryPO(List<CategoryPO> categoryPOS) {
        //key dict:mall:category:list:by:parent_id:0
        //  dict:mall:category:list:by:parent_id:99
        //
        //  value list 一个一个的parent为0的CategoryPO
        saveCategoryPOByParentId(0L,categoryPOS);
    }

    @Override
    public void saveCategoryPO(Long parentId, List<CategoryPO> categoryPOS) {
        saveCategoryPOByParentId(parentId,categoryPOS);
    }

    private void saveCategoryPOByParentId(Long parentId,List<CategoryPO> categoryPOS){
        String key = MALL_CATEGORY_CATCH_KEY_BY_PARENT + parentId;
        ListOperations listOperations = redisTemplate.opsForList();
        for (CategoryPO categoryPO : categoryPOS) {
            //todo 时间格式暂时json序列化不了，先不解决设置为空
            categoryPO.setGmtCreate(null);
            categoryPO.setGmtModified(null);
            listOperations.rightPush(key,categoryPO);
        }
    }



    @Override
    public List<CategoryPO> getChildrenById(Long id) {
        return getCategoryByParentId(id);
    }

    @Override
    public List<CategoryPO> getTopCategory() {
        return getCategoryByParentId(0L);
    }

    private  List<CategoryPO> getCategoryByParentId(Long parentId){
        String key = MALL_CATEGORY_CATCH_KEY_BY_PARENT + parentId;
        //顶级是什么意思 parentID = 0
        // key list-by-parent:0  value list 存储的是parent为0的数据
        // key list-by-parent:1  value list 存储的是parent为1的数据
        return redisTemplate.opsForList().range(key,0,-1);
    }


}
